from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.viewsets import ModelViewSet

from apps.goods.models import SKU, GoodsCategory, SPU, SPUSpecification
from apps.meiduo_admin.serializers.sku import SkuSerializer, SkuCategoriesSerializer, SpuSerializer, \
    SPUSpecificationSerializer
from apps.meiduo_admin.utils import PageNum
from rest_framework.generics import ListAPIView,RetrieveAPIView


class SkuModelViewSet(ModelViewSet):
    # queryset =SKU.objects.all()
    def get_queryset(self):
        keyword=self.request.query_params.get('keyword')
        if keyword:
            return SKU.objects.filter(name__contains=keyword)
        else:
            return SKU.objects.all()
    serializer_class = SkuSerializer
    pagination_class = PageNum


class SkuCategories(ListAPIView):
    # queryset = GoodsCategory.objects.filter(parent_id__gt=37)
    queryset = GoodsCategory.objects.filter(subs=None)
    serializer_class =SkuCategoriesSerializer

class Spu(ListAPIView):
    queryset = SPU.objects.all()
    serializer_class =SpuSerializer


class SPUSpecificationAPIView(ListAPIView):
    serializer_class = SPUSpecificationSerializer
    def get_queryset(self):
        spu_id = self.kwargs.get('spu_id')
        return SPUSpecification.objects.filter(spu_id=spu_id)


# class SPUSpecificationAPIView(APIView):
#     def get(self,request,spu_id):
#         specs=SPUSpecification.objects.filter(spu_id=spu_id)
#         ser=SPUSpecificationSerializer(specs,many=True)
#         return Response(ser.data)




